<?php
	include_once("../webconfig.php");
	include_once("../DAO/DataHelper.php");
	include_once("../DAO/SanPham_DAO.php");
	include_once("../DAO/DanhMucSanPham_DAO.php");
	
	if(isset($_REQUEST["TenSanPham"]))
	{
		$TenSanPham = $_REQUEST["TenSanPham"];
		if(SanPham::KiemTraTenSanPham($TenSanPham))
		{
			echo"<label style='color:#0F0' id='nameproductvalid' class='error'>Tên hợp lệ</label>";
		}
		else
		{
			echo"<label style='color:#F00' class='error'>Tên sản phẩm này đã được sử dụng rồi</label>";
		}
	}
	
	if(isset($_REQUEST["MaSP"]) && isset($_REQUEST["Xoa"]))
	{
		
		SanPham::XoaSanPham($_REQUEST["MaSP"]);
	}
?>
<script type="text/javascript">
	var xmlhttp;
	
	function GetXmlHttpObject()
	{
		if (window.XMLHttpRequest)
		{
			// code for IE7+, Firefox, Chrome, Opera, Safari
			return new XMLHttpRequest();
		}
		if (window.ActiveXObject)
		{
			// code for IE6, IE5
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		return null;
	}
	
	function CapNhat()
	{
		xmlhttp = GetXmlHttpObject();
		if(xmlhttp == null)
		{
			alert("Browser do not support HTTP REQUEST");
			return;
		}
		var IdSanPham = document.getElementById("idsanpham").value; 
		var GiaSanPham = document.getElementById("tbGiaSanPham").value;
		var DonViTinh = document.getElementById("tbDonViTinh").value;
		var SoLuong = document.getElementById("tbSoLuong").value;
		var MoTa = document.getElementById("tbMoTa").value;
		var MaDM = document.getElementById("cbTenDanhMuc").value;
		if(GiaSanPham != "" && DonViTinh != "" && SoLuong != "")
		{
			if(confirm('Bạn có muốn cập nhật sản phẩm không ?'))
			{
				
				var serverURL = "../BUS/xl_SanPham.php?MaSP=" + IdSanPham + "&CapNhat=1" + "&DonGia=" + GiaSanPham + "&DonViTinh=" + DonViTinh + "&SoLuong=" + SoLuong + "&MoTa=" + MoTa + "&MaDM=" + MaDM;
				xmlhttp.onreadystatechange=CapNhat_Change;
				xmlhttp.open("GET",serverURL,true);
				xmlhttp.send("null");
				
			}
		}
		else
		{
			alert("Vui lòng nhập đầy các thông tin yêu cầu");
			return false;
		}

	}
	
	function CapNhat_Change()
	{
		if (xmlhttp.readyState==4)
		{
			document.getElementById("detail_content").innerHTML=xmlhttp.responseText;
		}
	}
	
	function Sua()
	{
		xmlhttp = GetXmlHttpObject();
		if(xmlhttp == null)
		{
			alert("Browser do not support HTTP REQUEST");
			return;
		}
		var serverURL = "../BUS/xl_SanPham.php?MaSP=" + document.getElementById("idsanpham").value + "&Sua=1";
		xmlhttp.onreadystatechange=Sua_Change;
		xmlhttp.open("GET",serverURL,true);
		xmlhttp.send("null");
	}
	
	function Sua_Change()
	{
		if (xmlhttp.readyState==4)
		{
			document.getElementById("detail_content").innerHTML=xmlhttp.responseText;
		}
	}
	
	function HuyCapNhat()
	{
		xmlhttp = GetXmlHttpObject();
		if(xmlhttp == null)
		{
			alert("Browser do not support HTTP REQUEST");
			return;
		}
		if(confirm('Bạn không muốn cập nhật sản phẩm nữa ?'))
		{
			var serverURL = "../BUS/xl_SanPham.php?MaSP=" + document.getElementById("idsanpham").value + "&HuyCapNhat=1";
			xmlhttp.onreadystatechange=HuyCapNhat_Change;
			xmlhttp.open("GET",serverURL,true);
			xmlhttp.send("null");
		}
	}
	
	function HuyCapNhat_Change()
	{
		if (xmlhttp.readyState==4)
		{
			document.getElementById("detail_content").innerHTML=xmlhttp.responseText;
		}
	}
	
	$(document).ready(function(){
		$("#tbGiaSanPham").keyup(validNum).blur(validNum);
		$("#tbSoLuong").keyup(validNum).blur(validNum);
		function validNum() {
			var initVal = $(this).val();
			outputVal = initVal.replace(/[^0-9\.]/g,"").replace(/^\./,"");       
			if (initVal != outputVal) {
				$(this).val(outputVal);
			}
		}
	});
</script>
<?php
	if(isset($_REQUEST["MaSP"]) && isset($_REQUEST["CapNhat"]))
	{
		$listdanhmuc = DanhMucSanPham::DanhSachDanhMuc();
		$sanpham = new SanPham;
		$sanpham->sp_ma = $_REQUEST["MaSP"];
		$sanpham->sp_don_gia = $_REQUEST["DonGia"];
		$sanpham->sp_don_vi_tinh = $_REQUEST["DonViTinh"];
		$sanpham->sp_so_luong = $_REQUEST["SoLuong"];
		$sanpham->sp_mo_ta = $_REQUEST["MoTa"];
		$sanpham->sp_ma_danh_muc = $_REQUEST["MaDM"];
		SanPham::CapNhatSanPham($sanpham);
	}
?>


<?php
	if((isset($_REQUEST["MaSP"]) && isset($_REQUEST["HuyCapNhat"])) || (isset($_REQUEST["MaSP"]) && isset($_REQUEST["CapNhat"])))
	{
		$listdanhmuc = DanhMucSanPham::DanhSachDanhMuc();
		$sanpham = SanPham::ChiTietSanPham($_REQUEST["MaSP"]);
?>

	<form enctype="multipart/form-data" method="post">
        <table width="90%" height="525" border="0" align="center">
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><h4>THÔNG TIN SẢN PHẨM</h4></td>
            </tr>
            
            <tr>
                <td align="right" bgcolor="#CCCCCC" ><strong>Tên sản phẩm : </strong></td>
                <td bgcolor="#CCCCCC"><label style="font-weight:bold"><?php echo $sanpham->sp_ten; ?></label>
                <input type="hidden" id="idsanpham" value='<?php echo $sanpham->sp_ma; ?>'/>
                </td>
            </tr>
            <tr>
                <td align="right" bgcolor="#CCCCCC" ><strong>Ngày tạo : </strong></td>
                <td bgcolor="#CCCCCC"><label style="font-weight:bold"><?php echo $sanpham->sp_ngay_tao; ?></label></td>
            </tr>
            <tr>
                <td align="right" bgcolor="#F2F2F2"><strong>Danh Mục : </strong></td>
                <td bgcolor="#F2F2F2"><label style="font-weight:bold"><?php echo $sanpham->sp_ten_danh_muc; ?></label>
                </td>
            </tr>
            <tr>
                <td align="right" bgcolor="#F2F2F2"><strong>Giá tiền :</strong></td>
                <td bgcolor="#F2F2F2"><label style="font-weight:bold"><?php echo $sanpham->sp_don_gia; ?></label></td>
            </tr>
            <tr>
                <td align="right" bgcolor="#F2F2F2"><strong>Đơn vị tính : </strong></td>
                <td bgcolor="#F2F2F2"><label style="font-weight:bold"><?php echo $sanpham->sp_don_vi_tinh; ?></label></td>
            </tr>
            <tr>
                <td align="right" bgcolor="#CCCCCC"><strong>Số lượng : </strong></td>
                <td bgcolor="#CCCCCC"><label style="font-weight:bold"><?php echo $sanpham->sp_so_luong; ?></label></td>
            </tr>
                    
            <tr>
            <td align="right" bgcolor="#CCCCCC"><strong>Hình Mô Tả : </strong></td>
                <td bgcolor="#CCCCCC"><img src="<?php print GetUrl("images/$sanpham->sp_hinh_anh") ?>" />
                      
            </td>
                      
            </tr>
            <tr>
                <td height="70" align="right" bgcolor="#F2F2F2"><strong>Mô tả sản phẩm : </strong></td>
                <td bgcolor="#F2F2F2"><label style="font-weight:bold"><?php echo $sanpham->sp_mo_ta; ?></label></td>
            </tr>
                <td>&nbsp;</td>
                <td>
                    <div style="float:left;padding-left:100px">
                        <img src="../images/Edit-Document-icon.png" style="cursor:pointer" id="btsua" onclick="Sua()" />
                        <img src="../images/delete-icon1.png" style="cursor:pointer" name="delete" id="btdelete" onclick="Xoa()" />
                    </div>
                                                  
                </td>
            </tr>
        </table>
    </form>
<?php
	}
?>

<?php
	if(isset($_REQUEST["MaSP"]) && isset($_REQUEST["Sua"]))
	{
		$listdanhmuc = DanhMucSanPham::DanhSachDanhMuc();
		$sanpham = SanPham::ChiTietSanPham($_REQUEST["MaSP"]);
?>

	<form enctype="multipart/form-data" method="post">
        <table width="90%" height="525" border="0" align="center">
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="2" align="center"><h4>THÔNG TIN SẢN PHẨM</h4></td>
            </tr>
            <tr>
                <td align="right" bgcolor="#CCCCCC" ><strong>Tên sản phẩm : </strong></td>
                <td bgcolor="#CCCCCC"><label style="font-weight:bold"><?php echo $sanpham->sp_ten; ?></label>
                <input type="hidden" id="idsanpham" value='<?php echo $sanpham->sp_ma; ?>'/>
                </td>
            </tr>
            <tr>
              <td align="right" bgcolor="#F2F2F2"><strong>Danh Mục : </strong></td>
              <td bgcolor="#F2F2F2"><label>
                <select name="TenDanhMuc" id="cbTenDanhMuc" style="width:385px" >
                  <?php
                    
                    foreach($listdanhmuc as $danhmuc)
                    {
						if($sanpham->sp_ma_danh_muc == $danhmuc->dm_ma)
                        	echo "<option selected='selected' value='$danhmuc->dm_ma' >$danhmuc->dm_ten</option>";
						else
							echo "<option value='$danhmuc->dm_ma'>$danhmuc->dm_ten</option>";
                    }
                ?>	
                </select>
              </label></td>
            </tr>
            <tr>
              <td align="right" bgcolor="#F2F2F2"><strong>Giá tiền :</strong></td>
              <td bgcolor="#F2F2F2"><input name="GiaSanPham" type="text" value="<?php echo $sanpham->sp_don_gia ?>" id="tbGiaSanPham" size="60"  /><label style='color:#F00' class='error' style="display:inline">*</label></td>
            </tr>
            <tr>
              <td align="right" bgcolor="#F2F2F2"><strong>Đơn vị tính : </strong></td>
              <td bgcolor="#F2F2F2"><input name="DonViTinh" value="<?php echo $sanpham->sp_don_vi_tinh ?>" type="text" id="tbDonViTinh" size="60"  /><label style='color:#F00' class='error' style="display:inline">*</label></td>
            </tr>
            <tr>
              <td align="right" bgcolor="#CCCCCC"><strong>Số lượng : </strong></td>
              <td bgcolor="#CCCCCC"><input name="SoLuong" value="<?php echo $sanpham->sp_so_luong ?>" type="text" id="tbSoLuong" size="60" /><label style='color:#F00' class='error' style="display:inline">*</label></td>
            </tr>
            <tr>
              <td height="70" align="right" bgcolor="#F2F2F2"><strong>Mô tả sản phẩm : </strong></td>
              <td bgcolor="#F2F2F2"><textarea name="MoTa" id="tbMoTa" cols="45" rows="10" id="tbMota" ><?php echo $sanpham->sp_mo_ta ?></textarea></td>
            </tr>
            	<td></td>
              <td>
              	<div style="margin-left:300px">
              		<img src="../images/blog-post-accept-icon.png" style="cursor:pointer" id="btcapnhat" onclick="CapNhat()" />
                    <img src="../images/blog-post-delete-icon.png" style="cursor:pointer;" id="bthuycapnhat" onclick="HuyCapNhat()" />
              	</div>								  
              </td>
            </tr>
        </table>
    </form>
<?php
	}
?>